package com.ruoyi.web.controller.geo;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.EarthQuery;
import com.ruoyi.system.service.ITheGeographyService;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("geo")
public class GeoController {


    /**
     * 查询全球的地震信息
     * @param query
     * @return
     * @throws ParseException
     * @throws IOException
     */
    @GetMapping("data")
    public AjaxResult getData(GeoQuery query) throws ParseException, IOException {
        List<Earthquake> quakes = getQuakes();
        Calendar calendar = Calendar.getInstance();
        if (Objects.nonNull(query.getYearGt())) {
            quakes = quakes.stream().filter(q -> {
                calendar.setTime(q.getDate());
                return calendar.get(Calendar.YEAR) >= query.getYearGt();
            }).collect(Collectors.toList());
        }

        if (Objects.nonNull(query.getYearLt())) {
            quakes = quakes.stream().filter(q -> {
                calendar.setTime(q.getDate());
                return calendar.get(Calendar.YEAR) <= query.getYearLt();
            }).collect(Collectors.toList());
        }

        if (Objects.nonNull(query.getMonthGt())) {
            quakes = quakes.stream().filter(q -> {
                calendar.setTime(q.getDate());
                return calendar.get(Calendar.MONTH) >= query.getMonthGt();
            }).collect(Collectors.toList());
        }

        if (Objects.nonNull(query.getMonthLt())) {
            quakes = quakes.stream().filter(q -> {
                calendar.setTime(q.getDate());
                return calendar.get(Calendar.MONTH) <= query.getMonthLt();
            }).collect(Collectors.toList());
        }


        if (Objects.nonNull(query.getMagnitudeGt())) {
            quakes = quakes.stream().filter(q -> Float.parseFloat(q.getMagnitude()) >= query.getMagnitudeGt()).collect(Collectors.toList());
        }

        if (Objects.nonNull(query.getMagnitudeLt())) {
            quakes = quakes.stream().filter(q -> Float.parseFloat(q.getMagnitude()) <= query.getMagnitudeLt()).collect(Collectors.toList());
        }

        if (Objects.nonNull(query.getDepthGt())) {
            quakes = quakes.stream().filter(q -> Float.parseFloat(q.getDepth()) >= query.getDepthGt()).collect(Collectors.toList());
        }

        if (Objects.nonNull(query.getDepthLt())) {
            quakes = quakes.stream().filter(q -> Float.parseFloat(q.getDepth()) <= query.getDepthLt()).collect(Collectors.toList());
        }

        return AjaxResult.success(quakes);



    }

    /**
     * 将csv的文件数据转换成Earthquake对象信息
     * @return
     * @throws ParseException
     * @throws IOException
     */
    private static List<Earthquake> getQuakes() throws ParseException, IOException {
        List<Earthquake> result = new ArrayList<>();
        // 读取 CSV 文件
        ClassPathResource resource = new ClassPathResource("earthquakes.csv");

        try (InputStream inputStream = resource.getInputStream();
             BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
            reader.readLine();

            // 循环读取每行数据
            String line;
            while ((line = reader.readLine()) != null) {
                String[] fields = line.split(",");
                Earthquake earthquake = new Earthquake();
                earthquake.setDate(DateUtils.parseDate(fields[0], "dd/MM/yyyy"));
                earthquake.setTime(fields[1]);
                earthquake.setLatitude(fields[2]);
                earthquake.setLongitude(fields[3]);
                earthquake.setType(fields[4]);
                earthquake.setDepth(fields[5]);
                earthquake.setMagnitude(fields[6]);

                result.add(earthquake);
            }
        }

        return result;
    }


    @Autowired
    private ITheGeographyService iTheGeographyService;

    /**
     * 柱状图一 按年查询地震信息
     * @param query
     * @return
     */
    @GetMapping("getYear")
    public AjaxResult getYear(EarthQuery query) {
        List<List<String>>  list = iTheGeographyService.getYear(query);
        return AjaxResult.success(list);
    }

    /**
     * 柱状图二 按月查询地震信息
     * @param query
     * @return
     */
    @GetMapping("getMonth")
    public AjaxResult getMonth(EarthQuery query) {
        List<List<String>>  list = iTheGeographyService.getMonth(query);
        return AjaxResult.success(list);
    }

    /**
     * 柱状图三 查询每日的地震信息
     * @param query
     * @return
     */
    @GetMapping("getDay")
    public AjaxResult getDay(EarthQuery query) {
        List<List<String>>  list = iTheGeographyService.getDay(query);
        return AjaxResult.success(list);
    }

    /**
     * 饼状图一 查询地震的类型信息
     * @return
     */
    @GetMapping("getType")
    public AjaxResult getDay() {
        List<Map<String,String>>  list = iTheGeographyService.getType();
        return AjaxResult.success(list);
    }

    /**
     * 波点图一  获取震级和深度的关系信息
     * @return
     */
    @GetMapping("getSet")
    public AjaxResult getSet() {
        List<Object []>  list = iTheGeographyService.getSet();
        return AjaxResult.success(list);
    }


    /**
     * 根据经纬度坐标给数据赋值所属城市
     * @return
     */
    @GetMapping("setCity")
    public AjaxResult setCity() {
         iTheGeographyService.setCity();
        return AjaxResult.success(200);
    }


    /**
     * 全国地震信息
     * @param query
     * @return
     */
    @GetMapping("selectCity")
    public AjaxResult selectCity(EarthQuery query) {
        List<Map<String,Object>> list = iTheGeographyService.selectCity(query);
        return AjaxResult.success(list);
    }

    /**
     * 获取每个城市发生的地震信息
     * @param query
     * @return
     */
    @GetMapping("getCity")
    public AjaxResult getCity(EarthQuery query) {
        List<List<String>> list = iTheGeographyService.getCity(query);
        return AjaxResult.success(list);
    }


}
